import { Link,useParams} from "react-router-dom"
import data from '../data.js'
const limit = 5;

export default function PageNav() { 
  const { t = 'all', p = 1 } = useParams();
  let nowData = [];
  if (t==='all') {
    nowData = [...data['good'], ...data['share'], ...data['ask']];
  } else {
    nowData = data[t]
  }
  let pageLen = Math.ceil(nowData.length / limit);

  function getInner() {
    let inner = [];
    for (let i = 1; i <= pageLen; i++){
      inner.push(<Link key={ i} className={ parseInt(p)===i?'active':'' } to={`/index/${t}/${i}`}>{i}</Link>)
    }
    return inner;
  }
  
  return (
    <nav className="pagination">
      {parseInt(p) !== 1 ? <Link to={`/index/${t}/${parseInt(p)-1}`}>上一页</Link> : ''}
      {getInner()}
      {parseInt(p) !== pageLen ?<Link to={`/index/${t}/${parseInt(p)+1}`}>下一页</Link>:''}
  </nav>
  )
}